<template>
  <el-container>
    <el-header>
      <div class="project">后台管理系统</div>
      <el-menu class="el-menu-demo" mode="horizontal" background-color="#348dbc" text-color="#fff">
        <el-menu-item index="1">我的工作台</el-menu-item>
        <el-menu-item index="2">消息中心</el-menu-item>
        <el-menu-item index="3">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <el-avatar size="small" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png">
              </el-avatar><i class="user">张三</i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-plus" command="person">个人中心</el-dropdown-item>
              <el-dropdown-item icon="el-icon-circle-plus" command="password">修改密码</el-dropdown-item>
              <el-dropdown-item icon="el-icon-circle-plus-outline" command="quit">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu default-active="11" class="el-menu-vertical-demo" background-color="#2f4050" text-color="#fff"
          active-text-color="#ffd04b" :unique-opened="true" :collapse="false" :router="true">
          <!-- 一级菜单 -->
          <el-submenu :index="menu.index" v-for="menu in menuList" :key="menu.index">
            <template slot="title">
              <i :class="menu.icon"></i>
              <span>{{menu.name}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="menuItem.index" v-for="menuItem in menu.children" :key="menuItem.index">
              <i :class="menuItem.icon"></i>
              <span>{{menuItem.name}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: 'HomeView',
    data() {
      return {
        menuList: []
      }
    },
    mounted() {
      this.getMemuList()
    },
    methods: {
      async getMemuList() {
        const {
          data: res
        } = await this.$axios.get('/getMenuList')
        if (res.code !== 200) return this.$message.error('获取菜单失败!!!')
        this.menuList = res.result
      },
      handleCommand(command) {
        if (command == 'quit') {
          this.$confirm('此操作将退出系统, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            sessionStorage.clear()
            this.$router.push("/")
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消退出'
            });
          });
        }
      }
    }
  }
</script>

<style scoped="scoped">
  .el-container {
    height: 100%;
  }

  .el-header {
    background-color: #3c8dbc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
  }

  .el-header .project {
    width: 20%;
    color: #fff;
    font-size: 20px;
  }

  .el-aside {
    background-color: #2f4050;
  }

  .el-main {
    background-color: #FFFFFF;
  }

  .el-menu {
    border-right: solid 0px #e6e6e6;
  }

  .el-menu-demo {
    width: 100%;
    display: flex;
    justify-content: right;
  }

  .user {
    color: #fff;
    margin-left: 5px;
  }
</style>

/*
1.在el-menu标签中添加
unique-opened 是否只保持一个子菜单的展开
default-active 当前激活菜单的 index
2.设置el-menu的右边框
.el-menu {
border-right: solid 0px #e6e6e6;
}
3.设置el-menu路由模式

*/
